﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vgo_vs01.Models.DoiTuong;
using System.Net.Mail;
using System.Net;

namespace vgo_vs01.Models.KetNoi
{
    public class DaoCuModels
    {
        public void TaoDaoCu(DaoCu daocu)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_taodaocu @MaVatPham=N'" + daocu.MaVatPham + "'" + ",@SoLuong='" + daocu.Soluong + "'" + ",@TrangThai='" + daocu.Trangthai + "'" + ",@MaThanhVien='" + daocu.Mathanhvien + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                con.CloseData();
                con = null;
            }
        }
        #region Gửi mail
        public bool KiemTraEmail(string email)
        {
            bool kt = false;
            ConnectDB con = new ConnectDB();
            string sql = "KTmail @email=N'" + email + "'";
            con.ExeReader(sql);
            try
            {
                while (con.dataRead.Read())
                {
                    ThanhVien tv = new ThanhVien();
                    tv.Email = con.dataRead["Email"].ToString();
                    if (email == tv.Email)
                    {
                        kt = true;
                    }
                    else
                    {
                        kt = false;
                    }

                }
            }
            catch (Exception ex)
            {
            }

            finally
            {
                con.CloseData();
                con = null;
            }
            return kt;

        }
        public string SendMailFull(string mailFrom, string mailpass, string host, string port, string mailTo, string subject, string content, bool enableSsl)
        {
            try
            {
                var msg = new MailMessage
                {
                    IsBodyHtml = true,
                    Body = content,
                    From = new MailAddress(mailFrom, mailFrom)
                };
                msg.To.Add(new MailAddress(mailTo));
                msg.Subject = subject;

                var client = new SmtpClient(host, int.Parse(port))
                {
                    Credentials =
                        new NetworkCredential(mailFrom, mailpass),
                    EnableSsl = enableSsl
                };

                client.Send(msg);

                return "";
            }
            finally
            { }
        }

        #endregion
        //hàm lấy teenDannhap thanh vien
        public string GetUserName(int MaThanhVien)
        {
            string username ="";
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            try 
            {
                strSQL = "sp_getUserName @MaThanhVien=" +MaThanhVien+ "";
                objData.ExeReader(strSQL);
                if(objData.dataRead.Read())
                {   
                    username = objData.dataRead["TenDangNhap"].ToString();
                    return username;
                }
            } catch(Exception ex)
            {
                Console.WriteLine(ex);
              
            }
            finally
            {
                objData.CloseData();
                objData = null; 
            }
            return username;
        }
        //Hàm lấy email theo ID
        public string GetEmail(int MaThanhVien) {
            string email = "";
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            try
            {
                strSQL = "sp_getEmail @MaThanhVien=" + MaThanhVien + "";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {
                    email = objData.dataRead["Email"].ToString();
                    return email;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return email;
        }
        public List<VatPham> LayVatPham()
        {

            List<VatPham> dsvp = new List<VatPham>();
            ConnectDB con = new ConnectDB();
            string sql = "select*from VatPham";
            con.ExeReader(sql);
            while (con.dataRead.Read())
            {
                VatPham vp = new VatPham();
                vp.MaVatPham = int.Parse(con.dataRead["MaVatPham"].ToString());
                vp.TenVatPham = con.dataRead["TenVatPham"].ToString();
                dsvp.Add(vp);

            }
            return dsvp;
        }
    }
}